<?php

namespace App\Models\Factory\V2;


use App\Models\AbsModelFactory;
use App\Models\Orm\AwMerchant;
use App\Models\Orm\Product;
use App\Models\Orm\mdse;
use App\Models\Orm\Project;
use App\Models\Factory\V2\ProductFactory;


/**
 * 专题数据展示
 */
class ProjectFactory extends AbsModelFactory
{

    /**
     *
     * @return array
     */
    public static function GetList()
    {
        $result = Project::where('status',1)->orderBy('sort','asc')->get();
        return $result ? :[];
    }

    /**
     * 触发专题获取数据
     */
    public static function Information($id, $linkType)
    {
        //根据专题ID查询所有该专题下的产品
        $product = Project::join('project_product','project.id','=','project_product.project_id')
            ->join('product','product.id', '=','project_product.product_id')
            ->select('product.id','product.pname','product.product_introduction','product.product_logo','product.min_algorithm','product.fastest_time','product.online','product.activity','product.minimum_amount','product.maximum_amount','product.interest_algorithm')
            ->where('project.id',$id)
            ->where('product.status',1)
            ->orderBy('project_product.sort','asc')
            ->get();
        //根据产品ID查询标签
        foreach ($product as $key => $value)
        {
            $product[$key]['labels'] = Product::join('product_labels','product.id','=','product_labels.product_id')
                                                ->join('labels','labels.id','=','product_labels.labels_id')
                                                ->select('labels.id','labels.name','labels.font','labels.background','labels.status')
                                                ->where('labels.status',1)
                                                ->where('product.id',$value['id'])
                                                ->get();
        }
        //根据专题ID查询商品
        /*$mdse = Project::join('project_mdse','project.id','=','project_mdse.project_id')
            ->join('mdse','mdse.id', '=','project_mdse.mdse_id')
            ->select('mdse.mdse_ad','mdse.mdse_h5_link','mdse.id','mdse.mdse_name')
            ->where('mdse.status',1)
            ->where('project.id',$id)
            ->get();*/

        // 根据专题ID查询商品(新)
        $mdse = AwMerchant::from('aw_merchant as m')
            ->select('m.id', 'm.name as mdse_name', 'm.img as mdse_ad', 'm.link as mdse_h5_link')
            ->join('aw_project_merchant as pm', 'pm.merchant_id', '=', 'm.id')
            ->join('project as p', 'p.id', '=', 'pm.project_id')
            ->where('p.id', $id)
            ->get();

        $project['tenant_name'] = Project::where('id',$id)->first()->tenant_name;
        $project['project'] = Project::where('status',1)->where('id',$id)->get();
        // 添加产品多链接状态条件
        foreach ($product as $k => $v) {
            if(!ProductFactory::getlink($v['id'], $linkType)) {
                unset($product[$k]);
            }
        }
        $project['product'] = $product;
        $project['mdse']    = $mdse;

        return $project ? :[];

    }


}
